<!DOCTYPE html>
<html class="writer-html5" lang="en">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Clang Utils &mdash; MetronomeStatic 0.1.0 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=19f00094" />

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../_static/jquery.js?v=5d32c60e"></script>
        <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js?v=2389946f"></script>
        <script src="../_static/doctools.js?v=888ff710"></script>
        <script src="../_static/sphinx_highlight.js?v=4825356b"></script>
    <script src="../_static/js/theme.js"></script>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Universal AST (UAST)" href="universal_ast.html" />
    <link rel="prev" title="Tutorial" href="_index.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            MetronomeStatic
              <img src="../_static/sky-cloud.png" class="logo" alt="Logo"/>
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../installation.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation.html#user-installation">User Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation.html#developer-installation">Developer Installation</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../introduction.html">Introduction</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="_index.html">Tutorial</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">Clang Utils</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#loading-source-code-printing-ast">Loading Source Code &amp; Printing AST</a></li>
<li class="toctree-l3"><a class="reference internal" href="#querying-the-ast-interactively">Querying The AST Interactively</a></li>
<li class="toctree-l3"><a class="reference internal" href="#querying-code-attributes">Querying Code Attributes</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#queries-for-variables">Queries for Variables</a></li>
<li class="toctree-l4"><a class="reference internal" href="#parsing-expressions">Parsing Expressions</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="universal_ast.html">Universal AST (UAST)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../api/_index.html">API Reference</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../api/melodie_generator.html">MelodieGenerator</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../api/melodie_generator.html#members-list">Members List</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../api/melodie_generator.html#MelodieFuncFlow.functional.MelodieFrozenGenerator"><code class="docutils literal notranslate"><span class="pre">MelodieFrozenGenerator</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/melodie_generator.html#MelodieFuncFlow.functional.MelodieGenerator"><code class="docutils literal notranslate"><span class="pre">MelodieGenerator</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../api/common_utils.html">CommonUtils</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../api/common_utils.html#tips-before-reading">Tips Before Reading</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/common_utils.html#members-list">Members List</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../api/common_utils.html#MetronomeStatic.utils.MelodieFrozenGenerator"><code class="docutils literal notranslate"><span class="pre">MelodieFrozenGenerator</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/common_utils.html#MetronomeStatic.utils.MelodieGenerator"><code class="docutils literal notranslate"><span class="pre">MelodieGenerator</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/common_utils.html#MetronomeStatic.utils.abspath_from_current_file"><code class="docutils literal notranslate"><span class="pre">abspath_from_current_file()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/common_utils.html#MetronomeStatic.utils.compose"><code class="docutils literal notranslate"><span class="pre">compose()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/common_utils.html#MetronomeStatic.utils.file_to_dataurl"><code class="docutils literal notranslate"><span class="pre">file_to_dataurl()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/common_utils.html#MetronomeStatic.utils.parse_dataurl"><code class="docutils literal notranslate"><span class="pre">parse_dataurl()</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../api/clang_utils.html">ClangUtils</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../api/clang_utils.html#tips-before-reading">Tips Before Reading</a></li>
<li class="toctree-l3"><a class="reference internal" href="../api/clang_utils.html#members-list">Members List</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.ClassDefModel"><code class="docutils literal notranslate"><span class="pre">ClassDefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.DefModel"><code class="docutils literal notranslate"><span class="pre">DefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.FieldDefModel"><code class="docutils literal notranslate"><span class="pre">FieldDefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.FunctionDefModel"><code class="docutils literal notranslate"><span class="pre">FunctionDefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.ParamDefModel"><code class="docutils literal notranslate"><span class="pre">ParamDefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.StructDefModel"><code class="docutils literal notranslate"><span class="pre">StructDefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.TraversalContext"><code class="docutils literal notranslate"><span class="pre">TraversalContext</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.TypeDefModel"><code class="docutils literal notranslate"><span class="pre">TypeDefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.TypeWrapper"><code class="docutils literal notranslate"><span class="pre">TypeWrapper</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.UnaryOpPos"><code class="docutils literal notranslate"><span class="pre">UnaryOpPos</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.UnionDefModel"><code class="docutils literal notranslate"><span class="pre">UnionDefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.VarDefModel"><code class="docutils literal notranslate"><span class="pre">VarDefModel</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.all_globals"><code class="docutils literal notranslate"><span class="pre">all_globals()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.beautified_print_ast"><code class="docutils literal notranslate"><span class="pre">beautified_print_ast()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.build_call_graph"><code class="docutils literal notranslate"><span class="pre">build_call_graph()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.extract_ast"><code class="docutils literal notranslate"><span class="pre">extract_ast()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.get_compound_assignment_operator"><code class="docutils literal notranslate"><span class="pre">get_compound_assignment_operator()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.get_func_decl"><code class="docutils literal notranslate"><span class="pre">get_func_decl()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.get_func_decl_all"><code class="docutils literal notranslate"><span class="pre">get_func_decl_all()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.get_global_ref_names"><code class="docutils literal notranslate"><span class="pre">get_global_ref_names()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.get_global_refs"><code class="docutils literal notranslate"><span class="pre">get_global_refs()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.get_local_var_defs"><code class="docutils literal notranslate"><span class="pre">get_local_var_defs()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.get_param_decls"><code class="docutils literal notranslate"><span class="pre">get_param_decls()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.get_var_refs"><code class="docutils literal notranslate"><span class="pre">get_var_refs()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.is_function_definition"><code class="docutils literal notranslate"><span class="pre">is_function_definition()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.iter_ast"><code class="docutils literal notranslate"><span class="pre">iter_ast()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.iter_ast_from_file"><code class="docutils literal notranslate"><span class="pre">iter_ast_from_file()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.iter_files"><code class="docutils literal notranslate"><span class="pre">iter_files()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.parse_file"><code class="docutils literal notranslate"><span class="pre">parse_file()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.program_model_unparse"><code class="docutils literal notranslate"><span class="pre">program_model_unparse()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.split_binary_operator"><code class="docutils literal notranslate"><span class="pre">split_binary_operator()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.split_compound_assignment"><code class="docutils literal notranslate"><span class="pre">split_compound_assignment()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.split_for_loop_conditions"><code class="docutils literal notranslate"><span class="pre">split_for_loop_conditions()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.split_unary_operator"><code class="docutils literal notranslate"><span class="pre">split_unary_operator()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.traversal"><code class="docutils literal notranslate"><span class="pre">traversal()</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/clang_utils.html#MetronomeStatic.clang_utils.code_attributes.traversal_with_callback"><code class="docutils literal notranslate"><span class="pre">traversal_with_callback()</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../api/universal_ast.html">Universal AST</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../api/universal_ast.html#members-list">Members List</a><ul>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Additive"><code class="docutils literal notranslate"><span class="pre">Additive</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.And"><code class="docutils literal notranslate"><span class="pre">And</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Annotation"><code class="docutils literal notranslate"><span class="pre">Annotation</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.AnnotationDeclaration"><code class="docutils literal notranslate"><span class="pre">AnnotationDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.AnnotationMember"><code class="docutils literal notranslate"><span class="pre">AnnotationMember</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.AnnotationMethodDeclaration"><code class="docutils literal notranslate"><span class="pre">AnnotationMethodDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ArrayAccess"><code class="docutils literal notranslate"><span class="pre">ArrayAccess</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ArrayCreation"><code class="docutils literal notranslate"><span class="pre">ArrayCreation</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ArrayInitializer"><code class="docutils literal notranslate"><span class="pre">ArrayInitializer</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Assert"><code class="docutils literal notranslate"><span class="pre">Assert</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Assignment"><code class="docutils literal notranslate"><span class="pre">Assignment</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.BinaryExpression"><code class="docutils literal notranslate"><span class="pre">BinaryExpression</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Block"><code class="docutils literal notranslate"><span class="pre">Block</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Break"><code class="docutils literal notranslate"><span class="pre">Break</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Cast"><code class="docutils literal notranslate"><span class="pre">Cast</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Catch"><code class="docutils literal notranslate"><span class="pre">Catch</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ClassDeclaration"><code class="docutils literal notranslate"><span class="pre">ClassDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ClassInitializer"><code class="docutils literal notranslate"><span class="pre">ClassInitializer</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ClassLiteral"><code class="docutils literal notranslate"><span class="pre">ClassLiteral</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.CompilationUnit"><code class="docutils literal notranslate"><span class="pre">CompilationUnit</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Conditional"><code class="docutils literal notranslate"><span class="pre">Conditional</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ConditionalAnd"><code class="docutils literal notranslate"><span class="pre">ConditionalAnd</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ConditionalOr"><code class="docutils literal notranslate"><span class="pre">ConditionalOr</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ConstructorDeclaration"><code class="docutils literal notranslate"><span class="pre">ConstructorDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ConstructorInvocation"><code class="docutils literal notranslate"><span class="pre">ConstructorInvocation</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Continue"><code class="docutils literal notranslate"><span class="pre">Continue</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.DefaultStatement"><code class="docutils literal notranslate"><span class="pre">DefaultStatement</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.DereferenceExpr"><code class="docutils literal notranslate"><span class="pre">DereferenceExpr</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.DoWhile"><code class="docutils literal notranslate"><span class="pre">DoWhile</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Empty"><code class="docutils literal notranslate"><span class="pre">Empty</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.EmptyDeclaration"><code class="docutils literal notranslate"><span class="pre">EmptyDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.EnumConstant"><code class="docutils literal notranslate"><span class="pre">EnumConstant</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.EnumDeclaration"><code class="docutils literal notranslate"><span class="pre">EnumDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Equality"><code class="docutils literal notranslate"><span class="pre">Equality</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Expression"><code class="docutils literal notranslate"><span class="pre">Expression</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ExpressionStatement"><code class="docutils literal notranslate"><span class="pre">ExpressionStatement</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.FieldAccess"><code class="docutils literal notranslate"><span class="pre">FieldAccess</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.FieldDeclaration"><code class="docutils literal notranslate"><span class="pre">FieldDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.For"><code class="docutils literal notranslate"><span class="pre">For</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ForEach"><code class="docutils literal notranslate"><span class="pre">ForEach</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.FormalParameter"><code class="docutils literal notranslate"><span class="pre">FormalParameter</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.FuncCall"><code class="docutils literal notranslate"><span class="pre">FuncCall</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.GoToStatement"><code class="docutils literal notranslate"><span class="pre">GoToStatement</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.IfThenElse"><code class="docutils literal notranslate"><span class="pre">IfThenElse</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ImportDeclaration"><code class="docutils literal notranslate"><span class="pre">ImportDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.InstanceCreation"><code class="docutils literal notranslate"><span class="pre">InstanceCreation</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.InstanceOf"><code class="docutils literal notranslate"><span class="pre">InstanceOf</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.InterfaceDeclaration"><code class="docutils literal notranslate"><span class="pre">InterfaceDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Label"><code class="docutils literal notranslate"><span class="pre">Label</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Literal"><code class="docutils literal notranslate"><span class="pre">Literal</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.MethodDeclaration"><code class="docutils literal notranslate"><span class="pre">MethodDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Multiplicative"><code class="docutils literal notranslate"><span class="pre">Multiplicative</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Name"><code class="docutils literal notranslate"><span class="pre">Name</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Or"><code class="docutils literal notranslate"><span class="pre">Or</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.PackageDeclaration"><code class="docutils literal notranslate"><span class="pre">PackageDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.ReferenceExpr"><code class="docutils literal notranslate"><span class="pre">ReferenceExpr</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Relational"><code class="docutils literal notranslate"><span class="pre">Relational</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Resource"><code class="docutils literal notranslate"><span class="pre">Resource</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Return"><code class="docutils literal notranslate"><span class="pre">Return</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Shift"><code class="docutils literal notranslate"><span class="pre">Shift</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.SourceElement"><code class="docutils literal notranslate"><span class="pre">SourceElement</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Statement"><code class="docutils literal notranslate"><span class="pre">Statement</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Switch"><code class="docutils literal notranslate"><span class="pre">Switch</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.SwitchCase"><code class="docutils literal notranslate"><span class="pre">SwitchCase</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Synchronized"><code class="docutils literal notranslate"><span class="pre">Synchronized</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Throw"><code class="docutils literal notranslate"><span class="pre">Throw</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Throws"><code class="docutils literal notranslate"><span class="pre">Throws</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Try"><code class="docutils literal notranslate"><span class="pre">Try</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Type"><code class="docutils literal notranslate"><span class="pre">Type</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.TypeParameter"><code class="docutils literal notranslate"><span class="pre">TypeParameter</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Unary"><code class="docutils literal notranslate"><span class="pre">Unary</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Variable"><code class="docutils literal notranslate"><span class="pre">Variable</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.VariableDeclaration"><code class="docutils literal notranslate"><span class="pre">VariableDeclaration</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.VariableDeclarator"><code class="docutils literal notranslate"><span class="pre">VariableDeclarator</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.While"><code class="docutils literal notranslate"><span class="pre">While</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Wildcard"><code class="docutils literal notranslate"><span class="pre">Wildcard</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.WildcardBound"><code class="docutils literal notranslate"><span class="pre">WildcardBound</span></code></a></li>
<li class="toctree-l4"><a class="reference internal" href="../api/universal_ast.html#MetronomeStatic.universal_ast.universal_ast_nodes.Xor"><code class="docutils literal notranslate"><span class="pre">Xor</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../changelog.html">Change Log</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../changelog.html#start-may-10-2023">Start (May. 10, 2023)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../contribution.html">Contribution</a></li>
<li class="toctree-l1"><a class="reference internal" href="../about.html">About</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">MetronomeStatic</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="_index.html">Tutorial</a></li>
      <li class="breadcrumb-item active">Clang Utils</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../_sources/tutorial/clang_utils.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="clang-utils">
<h1>Clang Utils<a class="headerlink" href="#clang-utils" title="Permalink to this heading"></a></h1>
<p>Submodule <cite>clang_utils</cite> is a low-level API taking advantage of Clang and its python binding
<cite>libclang</cite>, making it easier to extract program information from C/CPP source code.</p>
<p>Example files are stored in <code class="docutils literal notranslate"><span class="pre">docs/source/examples</span></code>.</p>
<section id="loading-source-code-printing-ast">
<h2>Loading Source Code &amp; Printing AST<a class="headerlink" href="#loading-source-code-printing-ast" title="Permalink to this heading"></a></h2>
<p>Now, we have C file <code class="docutils literal notranslate"><span class="pre">assets/load_ast.c</span></code></p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">;</span>
<span class="linenos">2</span>
<span class="linenos">3</span><span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="linenos">4</span><span class="p">{</span>
<span class="linenos">5</span><span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
<span class="linenos">6</span><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
<span class="linenos">7</span><span class="p">}</span>
</pre></div>
</div>
<p>The Python script <code class="docutils literal notranslate"><span class="pre">load_ast.py</span></code> can parse this C file and dump AST.</p>
<p>Note that <code class="docutils literal notranslate"><span class="pre">sys.path.append</span></code> expression is used to let the interpreter
know where <code class="docutils literal notranslate"><span class="pre">MetronomeStatic</span></code> package is placed when in development.
<strong>If you have installed</strong> <code class="docutils literal notranslate"><span class="pre">MetronomeStatic</span></code> <strong>by pip, expression</strong> <code class="docutils literal notranslate"><span class="pre">sys.path.append</span></code> <strong>is not necessary.</strong>
This is also true for the other Python scripts below.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kn">import</span> <span class="nn">json</span>
<span class="linenos"> 2</span><span class="kn">import</span> <span class="nn">sys</span>
<span class="linenos"> 3</span>
<span class="linenos"> 4</span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;../../..&quot;</span><span class="p">)</span>
<span class="linenos"> 5</span>
<span class="linenos"> 6</span><span class="kn">from</span> <span class="nn">MetronomeStatic</span> <span class="kn">import</span> <span class="n">abspath_from_current_file</span>
<span class="linenos"> 7</span><span class="kn">from</span> <span class="nn">MetronomeStatic.clang_utils</span> <span class="kn">import</span> <span class="n">parse_file</span><span class="p">,</span> <span class="n">extract_ast</span><span class="p">,</span> <span class="n">get_func_decl</span>
<span class="linenos"> 8</span>
<span class="linenos"> 9</span><span class="c1"># Load file by clang and get the translation unit</span>
<span class="linenos">10</span><span class="n">translation_unit</span> <span class="o">=</span> <span class="n">parse_file</span><span class="p">(</span><span class="n">abspath_from_current_file</span><span class="p">(</span><span class="s2">&quot;assets/load_ast.c&quot;</span><span class="p">,</span> <span class="vm">__file__</span><span class="p">))</span>
<span class="linenos">11</span>
<span class="linenos">12</span><span class="c1"># Get the cursor from Translation Unit</span>
<span class="linenos">13</span><span class="n">cursor</span> <span class="o">=</span> <span class="n">translation_unit</span><span class="o">.</span><span class="n">cursor</span>
<span class="linenos">14</span>
<span class="linenos">15</span><span class="c1"># Get the function declaration of &quot;main&quot;</span>
<span class="linenos">16</span><span class="n">main_func</span> <span class="o">=</span> <span class="n">get_func_decl</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="s2">&quot;main&quot;</span><span class="p">)</span>
<span class="linenos">17</span>
<span class="linenos">18</span><span class="c1"># Extract the JSON representation of Clang AST</span>
<span class="linenos">19</span><span class="n">ast</span> <span class="o">=</span> <span class="n">extract_ast</span><span class="p">(</span><span class="n">main_func</span><span class="p">)</span>
<span class="linenos">20</span>
<span class="linenos">21</span><span class="c1"># Print AST</span>
<span class="linenos">22</span><span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">ast</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
</pre></div>
</div>
<p>The output message below (some lines were omitted) showed that
we could convert Clang AST of function <code class="docutils literal notranslate"><span class="pre">main</span></code>  to JSON in this way.</p>
<div class="highlight-plaintext notranslate"><div class="highlight"><pre><span></span>{
    &quot;kindText&quot;: &quot;CursorKind.FUNCTION_DECL&quot;,
    ...
    &quot;spelling&quot;: &quot;main&quot;,
    &quot;children&quot;: [
        {
            ...
            &quot;children&quot;: [
                {
                    ...
                    &quot;children&quot;: [
                        {
                            &quot;kindText&quot;: &quot;CursorKind.VAR_DECL&quot;,
                            ...
                            &quot;spelling&quot;: &quot;b&quot;
                        },
                        {
                            &quot;kindText&quot;: &quot;CursorKind.VAR_DECL&quot;,
                            ...
                            &quot;spelling&quot;: &quot;c&quot;
                        }
                    ]
                },
                ...
                    &quot;kindText&quot;: &quot;CursorKind.BINARY_OPERATOR&quot;,
                    &quot;position&quot;: [
                        6,
                        12
                    ],
                ...
                        }
                    ]
                }
            ]
        }
    ]
}
</pre></div>
</div>
</section>
<section id="querying-the-ast-interactively">
<h2>Querying The AST Interactively<a class="headerlink" href="#querying-the-ast-interactively" title="Permalink to this heading"></a></h2>
<p>Querying AST can be annoying when handling a lot of recursions and loops.
In this framework, inspired by <strong>Joern</strong>, a Scala-based code-analysis framework,
we developed a functional API to access the AST.</p>
<p>For C file <code class="docutils literal notranslate"><span class="pre">assets/interactive_query.c</span></code> shown below:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span><span class="kt">int</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">)</span>
<span class="linenos">2</span><span class="p">{</span>
<span class="linenos">3</span><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="linenos">4</span><span class="p">}</span>
<span class="linenos">5</span>
<span class="linenos">6</span><span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">paramA</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">paramB</span><span class="p">)</span>
<span class="linenos">7</span><span class="p">{</span>
<span class="linenos">8</span><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">paramA</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">paramB</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">f</span><span class="p">(</span><span class="n">paramA</span><span class="p">,</span><span class="w"> </span><span class="n">paramB</span><span class="p">);</span>
<span class="linenos">9</span><span class="p">}</span>
</pre></div>
</div>
<p>To get the AST of function <code class="docutils literal notranslate"><span class="pre">main</span></code>, we could write code like this:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kn">import</span> <span class="nn">sys</span>
<span class="linenos"> 2</span>
<span class="linenos"> 3</span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;../../..&quot;</span><span class="p">)</span>
<span class="linenos"> 4</span>
<span class="linenos"> 5</span><span class="kn">from</span> <span class="nn">MetronomeStatic.clang_utils</span> <span class="kn">import</span> <span class="o">*</span>
<span class="linenos"> 6</span><span class="kn">from</span> <span class="nn">MetronomeStatic</span> <span class="kn">import</span> <span class="o">*</span>
<span class="linenos"> 7</span>
<span class="linenos"> 8</span><span class="n">path</span> <span class="o">=</span> <span class="n">abspath_from_current_file</span><span class="p">(</span><span class="s2">&quot;assets/interactive_query.c&quot;</span><span class="p">,</span> <span class="vm">__file__</span><span class="p">)</span>
<span class="linenos"> 9</span>
<span class="linenos">10</span><span class="sd">&quot;&quot;&quot;</span>
<span class="linenos">11</span><span class="sd">Code above is critical for running this example,</span>
<span class="linenos">12</span><span class="sd">and not necessary when using in an interactive environment (IPython/Jupyter).</span>
<span class="linenos">13</span>
<span class="linenos">14</span><span class="sd">↓↓ JUST FOCUS ON THE FOLLOWING CODE! ↓↓</span>
<span class="linenos">15</span><span class="sd">&quot;&quot;&quot;</span>
<span class="linenos">16</span>
<span class="linenos">17</span><span class="n">ret</span> <span class="o">=</span> <span class="p">(</span>
<span class="linenos">18</span>    <span class="n">iter_ast</span><span class="p">(</span><span class="n">parse_file</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">cursor</span><span class="p">)</span>
<span class="linenos">19</span>    <span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">c</span><span class="p">:</span> <span class="n">c</span><span class="o">.</span><span class="n">kind</span> <span class="o">==</span> <span class="n">CursorKind</span><span class="o">.</span><span class="n">FUNCTION_DECL</span> <span class="ow">and</span> <span class="n">c</span><span class="o">.</span><span class="n">spelling</span> <span class="o">==</span> <span class="s2">&quot;main&quot;</span><span class="p">)</span>
<span class="linenos">20</span>    <span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="linenos">21</span><span class="p">)</span>
<span class="linenos">22</span>
<span class="linenos">23</span><span class="nb">print</span><span class="p">(</span><span class="n">ret</span><span class="o">.</span><span class="n">kind</span><span class="p">,</span> <span class="n">ret</span><span class="o">.</span><span class="n">spelling</span><span class="p">)</span>
</pre></div>
</div>
<p>The output is:</p>
<div class="highlight-plaintext notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span>CursorKind.FUNCTION_DECL main
</pre></div>
</div>
</section>
<section id="querying-code-attributes">
<h2>Querying Code Attributes<a class="headerlink" href="#querying-code-attributes" title="Permalink to this heading"></a></h2>
<section id="queries-for-variables">
<h3>Queries for Variables<a class="headerlink" href="#queries-for-variables" title="Permalink to this heading"></a></h3>
<p>Now, we have C file <code class="docutils literal notranslate"><span class="pre">assets/query_variables.c</span></code>.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">;</span>
<span class="linenos">2</span>
<span class="linenos">3</span><span class="kt">int</span><span class="w"> </span><span class="nf">demo_1</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">d</span><span class="p">)</span>
<span class="linenos">4</span><span class="p">{</span>
<span class="linenos">5</span><span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
<span class="linenos">6</span><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">d</span><span class="p">;</span>
<span class="linenos">7</span><span class="p">}</span>
</pre></div>
</div>
<p>The Python script <code class="docutils literal notranslate"><span class="pre">query_variables.py</span></code> can parse this C file and query variables in this file or specific function.</p>
<p>In this script, we took some advantage of <strong>functional programming</strong>.</p>
<ul class="simple">
<li><p>For functions like <code class="docutils literal notranslate"><span class="pre">get_local_var_defs</span></code>, it returns a generator <code class="docutils literal notranslate"><span class="pre">SkyGenerator</span></code>, yielding a <code class="docutils literal notranslate"><span class="pre">clang.cindex.Cursor</span></code> object for each iteration.</p></li>
<li><p>This generator supports some common patterns of functional programming, such as <code class="docutils literal notranslate"><span class="pre">map</span></code>, <code class="docutils literal notranslate"><span class="pre">filter</span></code> and <code class="docutils literal notranslate"><span class="pre">head</span></code>. Visit <a class="reference external" href="../api/common_utils.html#MetronomeStatic.utils.SkyGenerator">here</a> for details.</p></li>
<li><p>Method <code class="docutils literal notranslate"><span class="pre">.attribute(&quot;spelling&quot;)</span></code> is a shortcut for <code class="docutils literal notranslate"><span class="pre">.map(lambda</span> <span class="pre">node:</span> <span class="pre">node.spelling)</span></code></p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kn">import</span> <span class="nn">sys</span>
<span class="linenos"> 2</span>
<span class="linenos"> 3</span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;../../..&quot;</span><span class="p">)</span>
<span class="linenos"> 4</span><span class="kn">from</span> <span class="nn">MetronomeStatic</span> <span class="kn">import</span> <span class="n">abspath_from_current_file</span>
<span class="linenos"> 5</span><span class="kn">from</span> <span class="nn">MetronomeStatic.clang_utils</span> <span class="kn">import</span> <span class="p">(</span>
<span class="linenos"> 6</span>    <span class="n">get_func_decl</span><span class="p">,</span>
<span class="linenos"> 7</span>    <span class="n">get_local_var_defs</span><span class="p">,</span>
<span class="linenos"> 8</span>    <span class="n">get_var_refs</span><span class="p">,</span>
<span class="linenos"> 9</span>    <span class="n">get_param_decls</span><span class="p">,</span>
<span class="linenos">10</span>    <span class="n">get_global_ref_names</span><span class="p">,</span>
<span class="linenos">11</span>    <span class="n">parse_file</span><span class="p">,</span>
<span class="linenos">12</span>    <span class="n">get_func_decl</span><span class="p">,</span>
<span class="linenos">13</span>    <span class="n">all_globals</span><span class="p">,</span>
<span class="linenos">14</span><span class="p">)</span>
<span class="linenos">15</span>
<span class="linenos">16</span>
<span class="linenos">17</span><span class="c1"># Load file by clang and get the translation unit</span>
<span class="linenos">18</span><span class="n">translation_unit</span> <span class="o">=</span> <span class="n">parse_file</span><span class="p">(</span>
<span class="linenos">19</span>    <span class="n">abspath_from_current_file</span><span class="p">(</span><span class="s2">&quot;assets/query_variables.c&quot;</span><span class="p">,</span> <span class="vm">__file__</span><span class="p">)</span>
<span class="linenos">20</span><span class="p">)</span>
<span class="linenos">21</span>
<span class="linenos">22</span><span class="c1"># Get the cursor to whole file from Translation Unit</span>
<span class="linenos">23</span><span class="n">cursor</span> <span class="o">=</span> <span class="n">translation_unit</span><span class="o">.</span><span class="n">cursor</span>
<span class="linenos">24</span>
<span class="linenos">25</span><span class="c1"># Get the cursor to function demo_1</span>
<span class="linenos">26</span><span class="n">func_decl</span> <span class="o">=</span> <span class="n">get_func_decl</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="s2">&quot;demo_1&quot;</span><span class="p">)</span>
<span class="linenos">27</span>
<span class="linenos">28</span><span class="c1">## VARIABLE DECLARATIONS</span>
<span class="linenos">29</span>
<span class="linenos">30</span><span class="c1"># extract global variables defined in file</span>
<span class="linenos">31</span><span class="n">global_variables</span> <span class="o">=</span> <span class="n">all_globals</span><span class="p">(</span><span class="n">cursor</span><span class="p">)</span><span class="o">.</span><span class="n">attributes</span><span class="p">(</span><span class="s2">&quot;spelling&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">to_set</span><span class="p">()</span>
<span class="linenos">32</span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Global variables:&quot;</span><span class="p">,</span> <span class="n">global_variables</span><span class="p">)</span>
<span class="linenos">33</span>
<span class="linenos">34</span><span class="c1"># extract local variables</span>
<span class="linenos">35</span><span class="n">local_variables</span> <span class="o">=</span> <span class="n">get_local_var_defs</span><span class="p">(</span><span class="n">func_decl</span><span class="p">)</span><span class="o">.</span><span class="n">attributes</span><span class="p">(</span><span class="s2">&quot;spelling&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">to_set</span><span class="p">()</span>
<span class="linenos">36</span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Local variables:&quot;</span><span class="p">,</span> <span class="n">local_variables</span><span class="p">)</span>
<span class="linenos">37</span>
<span class="linenos">38</span><span class="c1"># extract parameters</span>
<span class="linenos">39</span><span class="n">param_variables</span> <span class="o">=</span> <span class="n">get_param_decls</span><span class="p">(</span><span class="n">func_decl</span><span class="p">)</span><span class="o">.</span><span class="n">attributes</span><span class="p">(</span><span class="s2">&quot;spelling&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">to_set</span><span class="p">()</span>
<span class="linenos">40</span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Param variables:&quot;</span><span class="p">,</span> <span class="n">param_variables</span><span class="p">)</span>
<span class="linenos">41</span>
<span class="linenos">42</span><span class="c1">## VARIABLE REFERENCES</span>
<span class="linenos">43</span>
<span class="linenos">44</span><span class="c1"># extract variable References</span>
<span class="linenos">45</span><span class="n">referenced_variables</span> <span class="o">=</span> <span class="n">get_var_refs</span><span class="p">(</span><span class="n">func_decl</span><span class="p">)</span><span class="o">.</span><span class="n">attributes</span><span class="p">(</span><span class="s2">&quot;spelling&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">to_set</span><span class="p">()</span>
<span class="linenos">46</span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Referenced variables:&quot;</span><span class="p">,</span> <span class="n">referenced_variables</span><span class="p">)</span>
<span class="linenos">47</span>
<span class="linenos">48</span>
<span class="linenos">49</span><span class="c1"># extract global references</span>
<span class="linenos">50</span><span class="n">global_references</span> <span class="o">=</span> <span class="n">get_global_ref_names</span><span class="p">(</span><span class="n">func_decl</span><span class="p">)</span><span class="o">.</span><span class="n">to_set</span><span class="p">()</span>
<span class="linenos">51</span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Used global variables:&quot;</span><span class="p">,</span> <span class="n">global_references</span><span class="p">)</span>
</pre></div>
</div>
<p>As the output message shown below:</p>
<ul class="simple">
<li><p>In file <code class="docutils literal notranslate"><span class="pre">assets/query_variables.c</span></code>, global variable <code class="docutils literal notranslate"><span class="pre">a</span></code> was defined.</p></li>
<li><p>Function <code class="docutils literal notranslate"><span class="pre">demo_1</span></code> defined local variables <code class="docutils literal notranslate"><span class="pre">b</span></code> and <code class="docutils literal notranslate"><span class="pre">c</span></code> inside, had parameter variable <code class="docutils literal notranslate"><span class="pre">d</span></code>, and referenced global variable <code class="docutils literal notranslate"><span class="pre">a</span></code>.</p></li>
</ul>
<div class="highlight-plaintext notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span>Global variables: {&#39;a&#39;}
<span class="linenos">2</span>Local variables: {&#39;c&#39;, &#39;b&#39;}
<span class="linenos">3</span>Param variables: {&#39;d&#39;}
<span class="linenos">4</span>Referenced variables: {&#39;c&#39;, &#39;a&#39;, &#39;b&#39;, &#39;d&#39;}
<span class="linenos">5</span>Used global variables: {&#39;a&#39;}
</pre></div>
</div>
</section>
<section id="parsing-expressions">
<h3>Parsing Expressions<a class="headerlink" href="#parsing-expressions" title="Permalink to this heading"></a></h3>
<p>Now, we have C file <code class="docutils literal notranslate"><span class="pre">assets/parse_expression.c</span></code>.
This file contains a series of operators.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">;</span>
<span class="linenos"> 2</span>
<span class="linenos"> 3</span><span class="kt">int</span><span class="w"> </span><span class="nf">demo</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">d</span><span class="p">)</span>
<span class="linenos"> 4</span><span class="p">{</span>
<span class="linenos"> 5</span><span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
<span class="linenos"> 6</span><span class="w">    </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="linenos"> 7</span><span class="w">    </span><span class="n">d</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">a</span><span class="p">;</span>
<span class="linenos"> 8</span><span class="w">    </span><span class="n">d</span><span class="o">++</span><span class="p">;</span>
<span class="linenos"> 9</span><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">d</span><span class="p">;</span>
<span class="linenos">10</span><span class="p">}</span>
</pre></div>
</div>
<p>The Python script <code class="docutils literal notranslate"><span class="pre">parse_expression.py</span></code> can parse all binary/unary operators inside function <code class="docutils literal notranslate"><span class="pre">demo</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kn">import</span> <span class="nn">sys</span>
<span class="linenos"> 2</span>
<span class="linenos"> 3</span><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;../../..&quot;</span><span class="p">)</span>
<span class="linenos"> 4</span><span class="kn">from</span> <span class="nn">MetronomeStatic</span> <span class="kn">import</span> <span class="n">abspath_from_current_file</span>
<span class="linenos"> 5</span><span class="kn">from</span> <span class="nn">MetronomeStatic.clang_utils</span> <span class="kn">import</span> <span class="p">(</span>
<span class="linenos"> 6</span>    <span class="n">get_func_decl</span><span class="p">,</span>
<span class="linenos"> 7</span>    <span class="n">parse_file</span><span class="p">,</span>
<span class="linenos"> 8</span>    <span class="n">get_func_decl</span><span class="p">,</span>
<span class="linenos"> 9</span>    <span class="n">CursorKind</span><span class="p">,</span>
<span class="linenos">10</span>    <span class="n">split_binary_operator</span><span class="p">,</span>
<span class="linenos">11</span>    <span class="n">split_unary_operator</span><span class="p">,</span>
<span class="linenos">12</span>    <span class="n">print_tokens</span><span class="p">,</span>
<span class="linenos">13</span><span class="p">)</span>
<span class="linenos">14</span>
<span class="linenos">15</span>
<span class="linenos">16</span><span class="c1"># Load file by clang and get the translation unit</span>
<span class="linenos">17</span><span class="n">translation_unit</span> <span class="o">=</span> <span class="n">parse_file</span><span class="p">(</span>
<span class="linenos">18</span>    <span class="n">abspath_from_current_file</span><span class="p">(</span><span class="s2">&quot;assets/parse_expression.c&quot;</span><span class="p">,</span> <span class="vm">__file__</span><span class="p">)</span>
<span class="linenos">19</span><span class="p">)</span>
<span class="linenos">20</span>
<span class="linenos">21</span><span class="c1"># Get the cursor to whole file from Translation Unit</span>
<span class="linenos">22</span><span class="n">cursor</span> <span class="o">=</span> <span class="n">translation_unit</span><span class="o">.</span><span class="n">cursor</span>
<span class="linenos">23</span>
<span class="linenos">24</span><span class="c1"># Get the cursor to function demo</span>
<span class="linenos">25</span><span class="n">func_decl</span> <span class="o">=</span> <span class="n">get_func_decl</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="s2">&quot;demo&quot;</span><span class="p">)</span>
<span class="linenos">26</span>
<span class="linenos">27</span><span class="c1"># Walk the AST and parse the BINARY_OPERATOR/UNARY_OPERATOR</span>
<span class="linenos">28</span><span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">func_decl</span><span class="o">.</span><span class="n">walk_preorder</span><span class="p">():</span>
<span class="linenos">29</span>    <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">kind</span> <span class="o">==</span> <span class="n">CursorKind</span><span class="o">.</span><span class="n">BINARY_OPERATOR</span><span class="p">:</span>
<span class="linenos">30</span>        <span class="n">left</span><span class="p">,</span> <span class="n">op</span><span class="p">,</span> <span class="n">right</span> <span class="o">=</span> <span class="n">split_binary_operator</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
<span class="linenos">31</span>        <span class="nb">print</span><span class="p">(</span>
<span class="linenos">32</span>            <span class="sa">f</span><span class="s2">&quot;BINARY_OP left: </span><span class="si">{</span><span class="n">print_tokens</span><span class="p">(</span><span class="n">left</span><span class="p">)</span><span class="si">}</span><span class="s2">  op: </span><span class="si">{</span><span class="n">op</span><span class="si">}</span><span class="s2">  right: </span><span class="si">{</span><span class="n">print_tokens</span><span class="p">(</span><span class="n">right</span><span class="p">)</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
<span class="linenos">33</span>        <span class="p">)</span>
<span class="linenos">34</span>    <span class="k">elif</span> <span class="n">node</span><span class="o">.</span><span class="n">kind</span> <span class="o">==</span> <span class="n">CursorKind</span><span class="o">.</span><span class="n">UNARY_OPERATOR</span><span class="p">:</span>
<span class="linenos">35</span>        <span class="n">op</span><span class="p">,</span> <span class="n">expr</span><span class="p">,</span> <span class="n">pos</span> <span class="o">=</span> <span class="n">split_unary_operator</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
<span class="linenos">36</span>        <span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;UNARY_OP op: </span><span class="si">{</span><span class="n">op</span><span class="si">}</span><span class="s2">  expr: </span><span class="si">{</span><span class="n">print_tokens</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span><span class="si">}</span><span class="s2">  position: </span><span class="si">{</span><span class="n">pos</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>As the output shown below:</p>
<ul class="simple">
<li><dl class="simple">
<dt>For unary operator, the third returning parameter of <code class="docutils literal notranslate"><span class="pre">split_unary_operator</span></code> (local variable <code class="docutils literal notranslate"><span class="pre">pos</span></code>), indicated the position of operator.</dt><dd><ul>
<li><p>UnaryOpPos is an <code class="docutils literal notranslate"><span class="pre">Enum</span></code> with optional values <code class="docutils literal notranslate"><span class="pre">UnaryOpPos.AFTER</span></code> and <code class="docutils literal notranslate"><span class="pre">UnaryOpPos.BEFORE</span></code>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">UnaryOpPos.AFTER</span></code> indicates that the operator appears after the operated expression, such as <code class="docutils literal notranslate"><span class="pre">p++</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">UnaryOpPos.BEFORE</span></code> indicates that the operator appears before the operated expression, such as <code class="docutils literal notranslate"><span class="pre">++i</span></code>, <code class="docutils literal notranslate"><span class="pre">&amp;p</span></code></p></li>
</ul>
</dd>
</dl>
</li>
<li><p>Assignment operator is one of binary operator</p></li>
</ul>
<div class="highlight-plaintext notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span>BINARY_OP left: c  op: =  right: a + b
<span class="linenos">2</span>BINARY_OP left: a  op: +  right: b
<span class="linenos">3</span>BINARY_OP left: d  op: =  right: &amp; a
<span class="linenos">4</span>UNARY_OP op: &amp;  expr: a  position: UnaryOpPos.BEFORE
<span class="linenos">5</span>UNARY_OP op: ++  expr: d  position: UnaryOpPos.AFTER
<span class="linenos">6</span>BINARY_OP left: c  op: *  right: d
</pre></div>
</div>
</section>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="_index.html" class="btn btn-neutral float-left" title="Tutorial" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="universal_ast.html" class="btn btn-neutral float-right" title="Universal AST (UAST)" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2022-2023, SkyGroup.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>